package com.itcast.demo_one

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.SimpleAdapter
import android.widget.Spinner
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class SimpleAdapterActivity : AppCompatActivity() {
    // 0. 数据类
    data class CityData(val icon: Int, val text: String, var intro: String)

    // 1. 定义城市列表
    private val cityList = listOf<CityData>(
        CityData(R.drawable.bj, "北京", "中国首都，历史悠久，政治文化中心，古韵今风交融。"),
        CityData(R.drawable.sh, "上海", "中国东部国际大都市，金融中心，多元文化汇聚。"),
        CityData(R.drawable.gz, "广州", "中国南部沿海城市，经济发达，文化多元。"),
        CityData(R.drawable.sz, "深圳", "中国南部科技创新中心，现代化城市。"),
        CityData(R.drawable.hz, "杭州", "中国东南部历史文化名城，风景秀丽。"),
        CityData(R.drawable.nj, "南京", "中国东部历史文化名城，教育科研中心。"),
    )

    lateinit var citySpinner: Spinner
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_simple_adapter)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        citySpinner = findViewById(R.id.spinner_icon)
        val adapter = SimpleAdapter(
            this,
            cityList.map { mapOf("icon" to it.icon,"text" to it.text, "intro" to it.intro) },
            R.layout.city_icon_item,
            arrayOf("icon", "text", "intro"),
            intArrayOf(R.id.cityIcon, R.id.cityText, R.id.cityIntro)
        )
        citySpinner.adapter = adapter
        citySpinner.setSelection(0)
        citySpinner.prompt = "请选择城市"

        // 监听选择
        citySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                val city = cityList[position]
                Toast.makeText(this@SimpleAdapterActivity, city.text, Toast.LENGTH_SHORT).show()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }
    }
}